layui.use(['form', 'table', 'element'], function () {
    var form = layui.form,
        layer = layui.layer,
        table = layui.table,
        $ = layui.$;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    // 当前弹出层，防止ID被覆盖
    var parentIndex = layer.index;

    //初始化
    $(function(){
        var id = $("#id").val();
        if(id != '' && id != null && id != undefined){
            var index = layer.load(2);
            var url = "/web/boInfoSet/getInfoById";
            $.get(url, {id: id}, function(data){
                layer.close(index);
                if(data.state == "200"){
                    $("#title").val(data.title);
                    $("#honeyTalk").val(data.honeyTalk);
                    $("#musicId").val(data.musicId);
                    $("#musicName").val(data.musicName);
                    if(data.isOpen == '1'){
                        $("#isOpenSwitch").attr("checked", "checked");
                        $("#isOpen").val('1');
                    }
                }else{
                    layer.msg(data.message, {icon:2});
                }
                form.render();
            })
        }
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        var index = layer.load(2);
        var url = "/web/boInfoSet/save";
        $.post(url, data.field, function(res){
            if(res.state == "200"){
                layer.msg(res.message, {icon:1});
            }else{
                layer.msg(res.message, {icon:2});
            }
            // 关闭弹出层
            layer.close(index);
            layer.close(parentIndex);
        }, 'json');

        return false;
    });

    //点击选择音乐按钮
    $("#musicBtn").on("click", function(){
        var musicId = $("#musicId").val();
        var musicName = $("#musicName").val();
        var content = layuimini.getHrefContent('/web/boInfoSet/chooseMusic?musicId=' + musicId + "&musicName=" + musicName);
        var index = layer.open({
            title: '选择音乐',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['80%','80%'], //宽高
            content: content
        });
        // $(window).on("resize", function () {
        //     layer.full(index);
        // });

        return true;
    });

    $("#closeBtn").on("click", function(){
        // 关闭弹出层
        layer.close(parentIndex);
    });

    //监听是否加锁开关
    form.on('switch(isOpenSwitch)', function(data){
        if(data.elem.checked){
            $("#isOpen").val("1"); //选中
        }else {
            $("#isOpen").val("0");
        }
    });

});